using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using Entities;
using System.Data.SqlClient;
using System.Data;
namespace DAL
{
    public class tbsanphamDAL
    {
        private SqlConnection cn = null;
        public tbsanphamDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);           
            cn.Open();
        }
        public void Insert(tbSanPham sanpham)
        {
            SqlCommand cm = new SqlCommand("SanPham_Insert", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IDLoaiSP", sanpham.IDLoaiSP);
            cm.Parameters.AddWithValue("@IDDiaDiem", sanpham.IDDiaDiem);
            cm.Parameters.AddWithValue("@IDHangSanXuat", sanpham.IDHangSanXuat);
            cm.Parameters.AddWithValue("@GiaBan", sanpham.GiaBan);
            cm.Parameters.AddWithValue("@GiaTriThuc", sanpham.GiaTriThuc);
            cm.Parameters.AddWithValue("@DieuKienSuDung", sanpham.DieuKienSuDung);
            cm.Parameters.AddWithValue("@MoTaSP", sanpham.MoTaSP);
            cm.Parameters.AddWithValue("@SoLuongSP", sanpham.SoLuongSP);
            cm.Parameters.AddWithValue("@SoLuongBan", sanpham.SoLuongBan);
            cm.Parameters.AddWithValue("@SoLuongDaBan", sanpham.SoLuongDaBan);
            cm.Parameters.AddWithValue("@IDTrangThaiSP", sanpham.IDTrangThaiSP);
            cm.Parameters.AddWithValue("@NgayBDBan", sanpham.NgayBDBan);
            cm.Parameters.AddWithValue("@NgayKTBan", sanpham.NgayKTBan);
            cm.Parameters.AddWithValue("@NgayCapNhat", sanpham.NgayCapNhat);
            cm.Parameters.AddWithValue("@IDNhanVien", sanpham.IDNhanVien);
            cm.Parameters.AddWithValue("@IDDoanhNghiep", sanpham.IDDoanhNghiep);
            cm.Parameters.AddWithValue("@TiLeHoaHong", sanpham.TiLeHoaHong);
            cm.ExecuteNonQuery();

        }
        public void Update(tbSanPham sanpham)
        {
            SqlCommand cm = new SqlCommand("SanPham_Update", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IDLoaiSP", sanpham.IDLoaiSP);
            cm.Parameters.AddWithValue("@IDDiaDiem", sanpham.IDDiaDiem);
            cm.Parameters.AddWithValue("@IDHangSanXuat", sanpham.IDHangSanXuat);
            cm.Parameters.AddWithValue("@GiaBan", sanpham.GiaBan);
            cm.Parameters.AddWithValue("@GiaTriThuc", sanpham.GiaTriThuc);
            cm.Parameters.AddWithValue("@DieuKienSuDung", sanpham.DieuKienSuDung);
            cm.Parameters.AddWithValue("@MoTaSP", sanpham.MoTaSP);
            cm.Parameters.AddWithValue("@SoLuongSP", sanpham.SoLuongSP);
            cm.Parameters.AddWithValue("@SoLuongBan", sanpham.SoLuongBan);
            cm.Parameters.AddWithValue("@SoLuongDaBan", sanpham.SoLuongDaBan);
            cm.Parameters.AddWithValue("@IDTrangThaiSP", sanpham.IDTrangThaiSP);
            cm.Parameters.AddWithValue("@NgayBDBan", sanpham.NgayBDBan);
            cm.Parameters.AddWithValue("@NgayKTBan", sanpham.NgayKTBan);
            cm.Parameters.AddWithValue("@NgayCapNhat", sanpham.NgayCapNhat);
            cm.Parameters.AddWithValue("@IDNhanVien", sanpham.IDNhanVien);
            cm.Parameters.AddWithValue("@IDDoanhNghiep", sanpham.IDDoanhNghiep);
            cm.Parameters.AddWithValue("@TiLeHoaHong", sanpham.TiLeHoaHong);
            cm.ExecuteNonQuery();
        }
        public void Delete(int Id)
        {
            SqlCommand cm = new SqlCommand("SanPham_Delete", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IDSanPham", Id);
            cm.ExecuteNonQuery();
        }
        public Entities.tbSanPham.tbSanPhamCollection Get(string Where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("SanPham_Get", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", Where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", System.Data.DbType.Int32)).Direction = System.Data.ParameterDirection.Output;
            SqlDataReader dr = cm.ExecuteReader();
            tbSanPham.tbSanPhamCollection sanphamcollection = new tbSanPham.tbSanPhamCollection();
            while (dr.Read())
            {
                tbSanPham sanpham = new tbSanPham();
                sanpham.IDSanPham = int.Parse(dr.GetValue(0).ToString());
                sanpham.TenSanPham = dr.GetValue(1).ToString();
                sanpham.IDLoaiSP =dr.GetValue(2).ToString();
                sanpham.IDDiaDiem = int.Parse(dr.GetValue(3).ToString());
                sanpham.IDHangSanXuat = dr.GetValue(4).ToString();
                sanpham.GiaBan = int.Parse(dr.GetValue(5).ToString());
                sanpham.GiaTriThuc =int.Parse( dr.GetValue(6).ToString());
                sanpham.DieuKienSuDung = dr.GetValue(7).ToString();
                sanpham.MoTaSP = dr.GetValue(8).ToString();
                sanpham.SoLuongSP = int.Parse(dr.GetValue(9).ToString());
                sanpham.SoLuongBan = int.Parse(dr.GetValue(10).ToString());
                sanpham.SoLuongDaBan = int.Parse(dr.GetValue(11).ToString());
                sanpham.IDTrangThaiSP = int.Parse(dr.GetValue(12).ToString());
                sanpham.NgayBDBan = DateTime.Parse(dr.GetValue(13).ToString());
                sanpham.NgayKTBan = DateTime.Parse(dr.GetValue(14).ToString());
                sanpham.NgayCapNhat = DateTime.Parse (dr.GetValue(15).ToString());
                sanpham.IDNhanVien = dr.GetValue(16).ToString();
                sanpham.IDDoanhNghiep =dr.GetValue(17).ToString();
                sanpham.TiLeHoaHong = float.Parse(dr.GetValue(18).ToString());
                sanphamcollection.Add(sanpham);                
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return sanphamcollection ;
        }
    }
}
